import axios from 'axios'
import {TOKEN_KEY,JS_VERSION_KEY} from './config'
import {JS_VERSION} from '@/js-version'
import store from '@/store'
import {removeCookie,getCookie} from '@/common/js/utils'
import Vue from 'vue'

axios.create({
  timeout: 1000 * 30,
  withCredentials: true,
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  }
})

/**
 * 请求拦截
 */

axios.interceptors.request.use(config => {
  // 每次部署前端后，需要重新修改js-version和数据库中的sys_js_version表中的js_version，并保证一致，才能让页面热部署，否则客户端有可能一直使用一个版本的前端
  config.headers[JS_VERSION_KEY] = JS_VERSION
	if (getCookie(TOKEN_KEY)) {
	    config.headers[TOKEN_KEY] = getCookie(TOKEN_KEY) // 请求头带上token
	}
  return config
}, error => {
  return Promise.reject(error)
})

/**
 * 响应拦截
 */
axios.interceptors.response.use(response => {
  return response
}, error => {
  return Promise.resolve(error.response)
})

/* http.interceptors.response.use(response => {
  // console.log(response)
  const res = response.data
  console.log(window.location)
  if (res && res.code === 401) { // 401, token失效
    removeToken()
    router.push({ path: '/login' }, () => {
      location.reload() // 刷新页面, 清空整站临时存储数据
    })
  }
  return Promise.resolve(res)
}, error => {
  return Promise.reject(error)
}) */
/**
 * get请求参数处理
 * @param {*} params 参数对象
 * @param {*} openDefultParams 是否开启默认参数?
 */
/* http.adornParams = (params = {}, openDefultParams = true) => {
  var defaults = {
    't': new Date().getTime()
  }
  return openDefultParams ? Object.assign(defaults, params) : params
} */

/**
 * post请求数据处理
 * @param {*} data 数据对象
 * @param {*} openDefultdata 是否开启默认数据?
 * @param {*} contentType 数据格式
 *  json: 'application/json; charset=utf-8'
 *  form: 'application/x-www-form-urlencoded; charset=utf-8'
 */
/* http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
  var defaults = {
    't': new Date().getTime()
  }
  data = openDefultdata ? Object.assign(defaults, data) : data
  return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
} */
const httpServer = (param) => {
  var defaults = {
    't': new Date().getTime()
  }
  let httpDefaultOpts = {
    method: param.method,
    url: param.url
  }
  if (param.method === 'get') {
    const params = !param.openDefultdata ? Object.assign(defaults, param.params) : param.params
    httpDefaultOpts.params = params
  } else {
    const data = !param.openDefultdata ? Object.assign(defaults, param.data) : param.data
    httpDefaultOpts.data = data
  }
  return new Promise((resolve, reject) => {
    axios(httpDefaultOpts).then(response => {
      const res = response.data
      if (res && res.code === 2021) {
        alert(res.msg)
        window.location.reload()
      }
      if (res && res.code === 401) {
        if (res.msg == 'invalid token') {
          removeCookie(TOKEN_KEY)
          window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname)
        }else{
          alert(res.msg)
          removeCookie(TOKEN_KEY)
          window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname)
        }
      }
      resolve(res)
    }).catch(error => {
      console.log(error)
      alert('网络异常')
      reject(error)
    })
  })
}
export default httpServer
